﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using CommunitySystem.DiaoDongService;
using CommunitySystem.EmpbasicService;
using EHR.Common;



namespace CommunitySystem
{
    public partial class AddZCEmpWindow : ChildWindow
    {
        private DiaoDongServiceClient dd = new DiaoDongServiceClient();
        private EmpBasicServiceClient Client = new EmpBasicServiceClient();
        private ObservableCollection<Emp_BasicInfoModel> selEmps = new ObservableCollection<Emp_BasicInfoModel>();
        private ZJZZRModel ZJZZR = new ZJZZRModel();
        public AddZCEmpWindow(CommunitySystem.DiaoDongService.ZJZZRModel zjzzr)
        {
            InitializeComponent();
            ZJZZR = zjzzr;
            Client.GetEmp_BasicInfoByConCompleted += new EventHandler<GetEmp_BasicInfoByConCompletedEventArgs>(Client_GetEmp_BasicInfoByConCompleted);
            Client.GetEmp_BasicInfoByConAsync(string.Format(" and orgid={0}", zjzzr.ZCDWID));
        }
        void Client_GetEmp_BasicInfoByConCompleted(object sender, GetEmp_BasicInfoByConCompletedEventArgs e)
        {
            var list = e.Result;
            foreach (var item in list)
            {
                StackPanel sp = new StackPanel();
                sp.Width = 100;
                sp.Margin = new Thickness(2);
                sp.Orientation = Orientation.Horizontal;
                CheckBox cbMonth = new CheckBox();
                cbMonth.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                cbMonth.Content = item.Name;
                cbMonth.Tag = item;
                cbMonth.Checked += new RoutedEventHandler(cbMonth_Checked);
                cbMonth.Unchecked += new RoutedEventHandler(cbMonth_Unchecked);

                sp.Children.Add(cbMonth);

                wra.Children.Add(sp);
            }
            //if (wra != null)
            //{
            //    foreach (var item in wra.Children)
            //    {
            //        if (((CheckBox)(((StackPanel)item).Children)[0]).IsChecked == false)
            //        {
            //            all.IsChecked = false;
            //            return;
            //        }
            //    }
            //}
        }

        void cbMonth_Unchecked(object sender, RoutedEventArgs e)
        {
            var ck = (sender as CheckBox).Tag as Emp_BasicInfoModel;
            if (selEmps.Where(p => p.ID == ck.ID).FirstOrDefault() != null)
                selEmps.Remove(ck);
        }

        void cbMonth_Checked(object sender, RoutedEventArgs e)
        {
            var ck = (sender as CheckBox).Tag as Emp_BasicInfoModel;
            if (selEmps.Where(p => p.ID == ck.ID).FirstOrDefault() == null)
                selEmps.Add(ck);
        }
      
        void cbqx_Unchecked(object sender, RoutedEventArgs e)
        {
            if (wra != null)
            {
                foreach (var item in wra.Children)
                {
                        ((CheckBox)(((StackPanel)item).Children)[0]).IsChecked = false;
                    
                }
            }
        }

        void cbqx_Checked(object sender, RoutedEventArgs e)
        {
            if (wra != null)
            {
                foreach (var item in wra.Children)
                {
                    ((CheckBox)(((StackPanel)item).Children)[0]).IsChecked = true;
                
                }
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            dd.AddZJZZR_EMPCompleted += new EventHandler<DiaoDongService.AddZJZZR_EMPCompletedEventArgs>(dd_AddZJZZR_EMPCompleted);
            var ZJZZREmp = new ObservableCollection<ZJZZR_Emp>();
            foreach (var item in selEmps)
            {
                var ZJZZREmpitem = new ZJZZR_Emp();
                ZJZZREmpitem.ZJZZRId = ZJZZR.Id;
                ZJZZREmpitem.YDWID = ZJZZR.ZCDWID;
                ZJZZREmpitem.YDW = ZJZZR.ZCDW;
                ZJZZREmpitem.YKS = item.DutyNo;
                ZJZZREmpitem.YZW = item.DutyName;
                ZJZZREmpitem.ZRDWID = ZJZZR.ZRDWID;
                ZJZZREmpitem.ZRDW = ZJZZR.ZRDW;
                ZJZZREmpitem.XKS = "";
                ZJZZREmpitem.XZW = "";
                ZJZZREmpitem.ZWCC = item.DutyLevel;
                ZJZZREmpitem.BZ = "未转任";
                ZJZZREmpitem.CreateDate = DateTime.Now.ToString();
                ZJZZREmpitem.CreateUser = Session.LoginUser.LoginName;
                ZJZZREmpitem.XM = item.Name;
                ZJZZREmpitem.EId = item.ID;
                ZJZZREmpitem.ZWSX = item.DutyAttribute;
                ZJZZREmp.Add(ZJZZREmpitem);
            }
            dd.AddZJZZR_EMPAsync(ZJZZREmp);
          
        }

        void dd_AddZJZZR_EMPCompleted(object sender, DiaoDongService.AddZJZZR_EMPCompletedEventArgs e)
        {
            if (e.Result)
                SilverlightMessageBoxLibrary.Message.InfoMessage("添加成功!");
            else SilverlightMessageBoxLibrary.Message.InfoMessage("添加失败!");

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

